package pokerserver.core;

import common.GameAction;
import common.TrunkatedPlayer;
import pokerserver.core.network.ServerThread;

/**
 * A class representing a player.
 * 
 * @author TDA367 project group 18
 */
public class Player implements IPlayer {

	/**
	 * Class variables
	 */
	private String nickName;
	private Hand hand;
	private GameAction action;
	private ServerThread playerThread;
	private int amountInPot = 0;
	private int position;
	private int stack;
	private boolean folded;

	/**
	 * 
	 * @param playerThread
	 */
	public Player(ServerThread playerThread) {
		this.playerThread = playerThread;
		this.action = GameAction.CONNECT;
		this.stack = 1500;
		this.amountInPot = 0;
	}

	@Override
	public void setPlayerThread(ServerThread playerThread) {
		this.playerThread = playerThread;
	}

	@Override
	public ServerThread getPlayerThread() {
		return playerThread;
	}

	@Override
	public void setPosition(int position) {
		this.position = position;
	}

	@Override
	public int getPosition() {
		return this.position;
	}

	@Override
	public int getAmountInPot() {
		return this.amountInPot;
	}

	@Override
	public void setAmountInPot(int amountInPot) {
		this.amountInPot = amountInPot;
	}

	@Override
	public void increaseAmountInPot(int bet) {
		this.amountInPot += bet;
		decreaseStack(bet);
	}
	/**
	 * when a player make a bet or call, he increase his private amount in pot, but decrease his own stack.
	 * @param sum, a int value, how much the stack will be decreased
	 */
	private void decreaseStack(int sum){
		this.stack -= sum;
	}

	@Override
	public String getNickName() {
		return nickName;
	}

	@Override
	public void setStack(int amount) {
		this.stack = amount;
	}

	@Override
	public Hand getHand() {
		if (this.hand == null) {
			return new Hand(null);
		}
		return hand;
	}

	@Override
	public void setHand(Hand hand) {
		this.hand = hand;
	}

	@Override
	public void setFolded(boolean status) {
		folded = status;
	}

	@Override
	public boolean isFolded() {
		return folded;
	}

	@Override
	public void setNickName(String nickName) {
		this.nickName = nickName;
	}

	@Override
	public GameAction getAction() {
		return this.action;
	}

	@Override
	public void setAction(GameAction action) {
		this.action = action;
	}

	@Override
	public int getStack() {
		return this.stack;
	}

	@Override
	public String toString() {
		return this.nickName;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((playerThread == null) ? 0 : playerThread.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof Player))
			return false;
		Player other = (Player) obj;
		if (playerThread == null) {
			if (other.playerThread != null)
				return false;
		} else if (!playerThread.equals(other.playerThread))
			return false;
		return true;
	}

	@Override
	public TrunkatedPlayer generateTrunkPlayer() {
		return new TrunkatedPlayer(position, nickName, action, amountInPot, stack);
	}

}
